--
-- Set privs by levels
--
-- Some part might be from https://github.com/DonBatman/myadmin

local modname = minetest.get_current_modname()
local S = minetest.get_translator(modname)

if not admin_ui.privlevels then admin_ui.privlevels = {} end

local levels = {}

levels.ghosted = {
  name=S("Ghosted"),
  clear=true,
  privs={
    [1] = {
      shout = true,        -- Can speak in chat
      -- noclip = true,       -- Can fly through solid nodes using noclip mode
      -- fly = true,          -- Can fly through solid nodes using noclip mode
      },
  }
}

levels.default = {
  name=S("Defautl player"),
  clear=true,
  privs={
    [1] = {
      shout = true,        -- Can speak in chat
      interact = true,     -- Can interact with things and modify the world
      },
  }
}

levels.helper = {
  name=S("Helper"),
  privs={
    [1] = {
      shout = true,        -- Can speak in chat
      home = true,         -- Can use /sethome and /home
      fast = true,         -- Can use fast mode
      interact = true,     -- Can interact with things and modify the world
      },
    [2] = {
      mesemaker = true,           -- Able to use all mesecons
      warp_user = true,           -- Allows use of warp points
      infotext_editor = true,     -- Able to edit nodes infotext with maker tool.
      help_reveal = true,         -- Allows you to reveal all hidden help entries with /help_reveal
      },
    [3] = {
      tunneling = true,           -- Allow use of tunnelmaker tool
      warp_admin = true,          -- Allows modification of warp points
      areas_high_limit = true,    -- Can protect more, bigger areas.
      },
    [4] = {
      worldedit = true,           -- Can use WorldEdit commands
      machines = true,            -- Playermachines work while not present on server
    }
  }
}



levels.moderator = {
  name=S("Moderator"),
  privs={
    [1] = {
      teleport = true,       -- Can teleport self
      settime = true,        -- Can set the time of day using /time
      },
    [2] = {
      kick = true,           -- Can kick players
      basic_privs = true,    -- Can modify basic privileges (shout, interact)
      },
    [3] = {
      bring = true,             -- Can teleport other players
      protection_bypass = true, -- Can bypass node protection in the world
      give = true,              -- Can use /give and /giveme
      areas = true,             -- Can administer areas.
      },
    [4] = {
      noclip = true,     -- Can fly through solid nodes using noclip mode
      ban = true,        -- Can ban and unban players
      include = true,    -- allow player to move in exclusion zone
      } 
  }
 }


levels.admin = {
  name=S("Admin"),
  privs={
    [1] = {
      privs = true,         -- Can modify privileges
      server = true,        -- Can do server maintenance stuff
      password = true,      -- Can use /setpassword and /clearpassword
      rollback = true,      -- Can use the rollback functionality
      debug = true,         -- Can enable wireframe
    }
  }
}

-- Those should be added by an in-game item
-- local excluded = {
    -- 'orehud',           -- Oretracker Orehud Priv
    -- 'xray',             -- Oretracker Xray Priv    
    -- 'peaceful_player',  -- Prevents Mobs Redo mobs from attacking player
    -- 'fly',              -- Can use fly mode
    -- 'creative',         -- Allow player to use creative inventory
-- }


function register_role(id, def)
  if not admin_ui.privlevels[id] then
    admin_ui.privlevels[id] = {
            name = def.name,
            clear = def.clear,
            privs = {},
          }
  end
end

function register_level(roleid, privs)
  local p = {}
  for k, v in pairs(privs) do
    if minetest.registered_privileges[k] then
      p[k] = v
    end
  end
  table.insert(admin_ui.privlevels[roleid].privs, p)
end

for k,v in pairs(levels) do
  register_role(k, v)
  for _,l in ipairs(v.privs) do
    register_level(k,l)
  end
end


















-- local level = {}
-- level.ghost = table.concat(level.player, { 'kick', 'teleport', 'fly')


-- level.player = { 'basic_privs', 'shout', 'interact', 'home', 'fast' }
-- level.moderator = table.concat(level.player, { 'kick', 'teleport', 'fly')
-- level.admin = table.concat(level.player, { 'kick', 'teleport', 'fly')
-- level.moderator =
-- level.silenced =
-- level.super_admin =
-- level.super_admin =
-- level.super_admin =

-- minetest.register_privilege("myadmin_ui.privlevels.", "Lets person set level of privlege people have")

-- minetest.register_privilege("myadmin_ui.privlevels._super", "Lets person set level of privlege people have plus the super level")

-- minetest.register_chatcommand("myprivs_commands", {
	-- privs = {myadmin_ui.privlevels. = true},
	-- func = function(name, param)
		-- minetest.chat_send_player(name,"Available commands - /admin, /mod, /helper, /norm, /punish, /unpunish, silence, /ghost")
		-- return true
	-- end,
-- })

-- minetest.register_chatcommand("super_admin", {
	-- params = "",
	-- description = "Super Administrator",
	-- privs={myadmin_ui.privlevels._super=true},
	-- func = function(name, param)
		-- if minetest.get_player_by_name(param) then
		-- minetest.set_player_privs(param, {})
		-- local privs=minetest.get_player_privs(param)
			-- privs.shout=true
			-- privs.interact=true
			-- privs.home=true
			-- privs.give=true
			-- privs.teleport=true
			-- privs.bring=true
			-- privs.fast=true
			-- privs.fly=true
			-- privs.noclip=true
			-- privs.privs=true
			-- privs.basic_privs=true
			-- privs.kick=true
			-- privs.ban=true
			-- privs.areas=true
			-- privs.myadmin_ui.privlevels.=true
			-- privs.tps_magicchests=true
			-- minetest.set_player_privs(param,privs)
			-- minetest.chat_send_player(param, "You are now a Super Admin")
			-- minetest.chat_send_player(name, param .. " is now a Super Admin")
			-- return true
		-- end
-- end})

-- minetest.register_chatcommand("admin", {
	-- params = "",
	-- description = "Administrator",
	-- privs={myadmin_ui.privlevels.=true},
	-- func = function(name, param)
		-- if minetest.get_player_by_name(param) then
		-- minetest.set_player_privs(param, {})
		-- local privs=minetest.get_player_privs(param)
			-- privs.shout=true
			-- privs.interact=true
			-- privs.home=true
			-- privs.teleport=true
			-- privs.bring=true
			-- privs.fast=true
			-- privs.fly=true
			-- privs.noclip=true
			-- privs.basic_privs=true
			-- privs.kick=true
			-- privs.ban=true
			-- privs.areas=true
			-- privs.myadmin_ui.privlevels.=true
			-- privs.tps_magicchests=true
			-- minetest.set_player_privs(param,privs)
			-- minetest.chat_send_player(param, "You are now an Admin")
			-- minetest.chat_send_player(name, param .. " is now an Admin")
			-- return true
		-- end
-- end})

-- minetest.register_chatcommand("mod", {
	-- params = "",
	-- description = "Moderator",
	-- privs={myadmin_ui.privlevels.=true},
	-- func = function(name, param)
		-- if minetest.get_player_by_name(param) then
		-- minetest.set_player_privs(param, {})
		-- local privs=minetest.get_player_privs(param)
			-- privs.shout=true
			-- privs.interact=true
			-- privs.home=true
			-- privs.teleport=true
			-- privs.fast=true
			-- privs.fly=true
			-- privs.basic_privs=true
			-- privs.kick=true
			-- minetest.set_player_privs(param,privs)
			-- minetest.chat_send_player(param, "You are now a Moderator")
			-- minetest.chat_send_player(name, param .. " is now a Moderator")
			-- return true
		-- end
-- end})

-- minetest.register_chatcommand("helper", {
	-- params = "",
	-- description = "Helper",
	-- privs={myadmin_ui.privlevels.=true},
	-- func = function(name, param)
		-- if minetest.get_player_by_name(param) then
		-- minetest.set_player_privs(param, {})
		-- local privs=minetest.get_player_privs(param)
			-- privs.shout=true
			-- privs.interact=true
			-- privs.home=true
			-- privs.fast=true
			-- privs.fly=true
			-- privs.kick=true
			-- minetest.set_player_privs(param,privs)
			-- minetest.chat_send_player(param, "You are now a Helper")
			-- minetest.chat_send_player(name, param .. " is now a Helper")
			-- return true
		-- end
-- end})

-- minetest.register_chatcommand("norm", {
	-- params = "",
	-- description = "Normal Player",
	-- privs={myadmin_ui.privlevels.=true},
	-- func = function(name, param)
		-- if minetest.get_player_by_name(param) then
		-- minetest.set_player_privs(param, {})
		-- local privs=minetest.get_player_privs(param)
			-- privs.shout=true
			-- privs.interact=true
			-- privs.home=true
			-- privs.fast=true
			-- minetest.set_player_privs(param,privs)
			-- minetest.chat_send_player(param, "You are now a Normal Player")
			-- minetest.chat_send_player(name, param .. " is now a Normal Player")
			-- return true
		-- end
-- end})

-- minetest.register_chatcommand("unpunish", {
	-- params = "",
	-- description = "Unpunish Player",
	-- privs={myadmin_ui.privlevels.=true},
	-- func = function(name, param)
		-- if minetest.get_player_by_name(param) then
		-- minetest.set_player_privs(param, {})
		-- local privs=minetest.get_player_privs(param)
			-- privs.shout=true
			-- privs.interact=true
			-- privs.home=true
			-- privs.fast=true
			-- minetest.set_player_privs(param,privs)
			-- minetest.chat_send_player(param, "You are now unpunished")
			-- minetest.chat_send_player(name, param .. " is now unpunished")
			-- return true
		-- end
-- end})

-- minetest.register_chatcommand("punish", {
	-- params = "",
	-- description = "Punish Player",
	-- privs={myadmin_ui.privlevels.=true},
	-- func = function(name, param)
		-- if minetest.get_player_by_name(param) then
		-- minetest.set_player_privs(param, {})
		-- local privs=minetest.get_player_privs(param)
			-- privs.shout=true
			-- minetest.set_player_privs(param,privs)
			-- minetest.chat_send_player(param, "You are now being punished")
			-- minetest.chat_send_player(name, param .. " is now punished")
			-- return true
		-- end
-- end})

-- minetest.register_chatcommand("silence", {
	-- params = "",
	-- description = "Silence Player",
	-- privs={myadmin_ui.privlevels.=true},
	-- func = function(name, param)
		-- if minetest.get_player_by_name(param) then
		-- minetest.set_player_privs(param, {})
		-- local privs=minetest.get_player_privs(param)
			-- privs.interact=true
			-- privs.home=true
			-- privs.fast=true
			-- minetest.set_player_privs(param,privs)
			-- minetest.chat_send_player(param, "You are now silenced")
			-- minetest.chat_send_player(name, param .. " is now silenced")
			-- return true
		-- end
-- end})

-- minetest.register_chatcommand("ghost", {
	-- params = "",
	-- description = "Remove all privs",
	-- privs={myadmin_ui.privlevels.=true},
	-- func = function(name, param)
		-- if minetest.get_player_by_name(param) then
		-- minetest.set_player_privs(param, {})
			-- minetest.chat_send_player(param, "You are now a ghost")
			-- minetest.chat_send_player(name, param .. " is now ghosted")
			-- return true
		-- end
-- end})
